如果網站需要上傳檔案或圖片的功能,除了carrierwave之外,也可以使用paperclip實現
以下介紹這個好用的gem
環境要求
要使用paperclip,ruby的版本必須是2.0以上,rails則是3.2之後
imagemagick也得先安裝,在OS X下,使用Homebrew安裝1
brew install imagemagick
安裝
1  | gem "paperclip", "~> 4.3"  | 
1  | > bundle install  | 
Model
設定model,假如有一個Pic model負責管理圖片1
2
3
4class Pic < ActiveRecord::Base
	has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
	validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
Migration
1  | > rails generate paperclip pic avatar  | 
Controller
1  | def create  | 
View
1  | <%= image_tag @pic.avatar.url %>  |